/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actualizador;

/**
 *
 * @author TomasQuinteros
 */

import java.sql.*;

public class OracleConn {
    
  private String        url     = "";
  private String        usr     = "AEL"; 
  private String        pswd    = ""; 
  private String        driver  = "oracle.jdbc.OracleDriver";
  private Connection    con;
  //private Connection    con2;  
  public  Statement     stmt    = null;
  public  ResultSet     rs      = null; 
  public  PreparedStatement stmtPrepare;
  public  PreparedStatement stmtPrepareLog;
  
  public Connection getConnection(){
      return con;
  }
  
  public void SetPswd(String ps) {
          this.pswd = ps;
    }
  
  public boolean Conectar() {
  boolean resultado = true;
  try {

         Class.forName(this.driver);
         AddLog("Conectando a base Oracle..."+this.getUrl());
         this.con = DriverManager.getConnection( this.getUrl(), this.getUsr(),this.pswd);
         //this.con.setAutoCommit(false);
         AddLog("Conexión exitosa a base Oracle..."+this.getUrl());
         resultado = true;
      }

      catch (SQLException se) {
         try {
            AddLog("Conexion a  Oracle  fallo, " +this.getUrl()+"-"+this.getUsr()+"-"+this.pswd);
            AddLog("Conexion a Oracle  fallo, " +se.toString());
            resultado = false;
         }
         catch (Exception e) {
            e.printStackTrace();
            resultado = false;
         }
      }
      catch (Exception e) {
         e.printStackTrace();
         resultado = false;
      }
      // Close the JDBC objects.

  return resultado;
      
   }
  
 public void Cerrarconexion()
  {
        if (this.con != null) 
             try { this.con.close(); } 
             catch(Exception e) {}
        //if (this.con2 != null) 
             //try { this.con2.close(); } 
             //catch(Exception e) {}      
  }
    
 public void InsertarTabla(ResultSet rsi,String qry, int n_param) {
   try {       
       while (rsi.next()) {
                this.stmtPrepare = this.con.prepareStatement(qry);            
                AddLog("EJECUTANDO QRY:"+qry);
                AddLog("!!!TRANSFIRIENDO DATOS..........!!!");
                AddLog("Insertando datos en tabla..."); 
             
                for (int i = 1; i <= n_param ; i++) {
                    this.stmtPrepare.setObject(i,rsi.getObject(i));
                } 
                
                this.stmtPrepare.executeUpdate();
                this.stmtPrepare.close();
                AddLog("Replicacion tabla ok...");             
        }
   }
    catch (SQLException se) {
            try {
                AddLog("error en InsertarTabla, " + se.toString());
                this.stmtPrepare.close();
            } catch (SQLException ex) { }
    }
                 
   }
      
 public void AddLog(String valor) {
      
      System.out.println(valor);
                 
}
            
 public void CerrarStatement(){
        try {
            this.stmt.close();
        } catch (SQLException ex) {
            
        }
 }        
    
 public ResultSet EjecutarSql(String qry) throws SQLException {
   try {    
            this.stmt  = this.con.createStatement();
            String SQL = qry;
            AddLog("ejecutando..."+qry);
            AddLog("Migración Exitosa!");
            this.stmt.executeUpdate(SQL);
            return this.rs;  
   }
    catch (SQLException se) {
            AddLog("error al grabar registros, " +se.toString());
                 throw se;
    }
                  
   }    
   
 public void CerrarLog() {
          
}

    /**
     * @return the usr
     */
    public String getUsr() {
        return usr;
    }

    /**
     * @param usr the usr to set
     */
    public void setUsr(String usr) {
        this.usr = usr;
    }

    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }
  
  
  
  
  
  
  
}
